# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : setDriverKeyframe_editor.py
# Date      : 2019/10/10 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------
import traceback

import maya.cmds as mc
import maya.mel as mel

import editors.editor as editor
import scripts.commonCmds as con
import scripts.animCurveCmds  as acc

reload(acc)
def showEditor():
    SetDriverKeyframeEditor().win()
    
class SetDriverKeyframeEditor(editor.Editor):
    def __init__(self):
        super(SetDriverKeyframeEditor,self).__init__()

        self.object_tf = "object_tf"
        self.obj_attr_tcl = "obj_attr_tcl"
        self.animCurveNode_tcl = "animCurveNode_tcl"
        self.connectObj_tcl = "connectObj_tcl"
        self.lf_tag_tf = "lf_tag_tf"
        self.rt_tag_tf = "rt_tag_tf"

    def win(self):
        _win = self.selfWindow()
        mc.window(_win,e=True,wh=[400,300],s=True)
        mc.menuBarLayout(bgc=self.rgbConvert(118, 137, 143))
        mc.menu("File", )
        mc.menuItem("Export Data...")
        mc.setParent("..")
        mc.columnLayout(adj=True)
        row= mc.rowLayout(nc=10,w=350)
        
        mc.columnLayout(adj=True,w=90)
        # self.object_tf = "object_tf"
        mc.text(" Driver Object =>")
        mc.textField(self.object_tf,ed=False,rfc=lambda *args:self.btn_object_tf_selectCmd())
        mc.button(l="Load",c=lambda *args:self.btn_load())
        mc.separator()
        lr_row = mc.rowLayout(nc=10,w=90)
        mc.textField(self.lf_tag_tf,w=43,tx="L_",ed=False)
        mc.textField(self.rt_tag_tf,w=43,tx="R_",ed=False)
        mc.setParent("..")
        mc.button(l="Mirror Obj Sdk",c=lambda *args:self.btn_mirrorObjSdk())
        mc.setParent("..")
        
        paneLayout = mc.paneLayout("dataPaneLayout",configuration='vertical3',w=300)
        
        # self.animCurveNode_tcl = "animCurveNode_tcl"
        mc.textScrollList(self.obj_attr_tcl,sc=lambda *args:self.btn_driverAttr_tcl_selectCmd())
        mc.textScrollList(self.animCurveNode_tcl,sc=lambda *args:self.btn_animCurveNode_tcl_selectCmd())
        mc.textScrollList(self.connectObj_tcl,sc=lambda *args:self.btn_connectObj_tcl_selectCmd())
        
        mc.setParent("..")
        mc.setParent("..")
        mc.separator()
        self.copyright()
        
        mc.setParent("..")
        mc.showWindow(_win)
        
        mc.paneLayout(paneLayout,e=True,ps=[2,0,0])
        # mc.paneLayout(paneLayout,e=True,ps=[1,100,0])
    
    def btn_load(self):
        
        sel = mc.ls(sl=True,fl=True)
        if sel:
            nt = mc.nodeType(sel[0])
            if nt not in ["unitConversion","blendWeighted"]+acc.sdkNodeType:
                mc.textField(self.object_tf,e=True,tx=sel[0])
                self.btn_refresh_driverAttr_tcl()
            else:
                pass
        return
    
    def btn_object_tf_selectCmd(self):
        tx = mc.textField(self.object_tf,q=True,tx=True)
        if tx:
            mc.select(tx)
        return
        
    def btn_refresh_driverAttr_tcl(self):
        mc.textScrollList(self.obj_attr_tcl,e=True,ra=True)
        obj = mc.textField(self.object_tf,q=True,tx=True)
        outputList = acc.find_driverAttr(obj)
        if outputList:
            mc.textScrollList(self.obj_attr_tcl, e=True, a=outputList)
            mc.textScrollList(self.obj_attr_tcl,e=True,sii=1)
            
            self.btn_driverAttr_tcl_selectCmd()
        return
    def btn_driverAttr_tcl_selectCmd(self):
        self.btn_refresh_animCurveNode_tcl()
        self.btn_refresh_connectObj_tcl()
        
        return
        
    def btn_refresh_animCurveNode_tcl(self):
        
        mc.textScrollList(self.animCurveNode_tcl,e=True,ra=True)
        
        obj = mc.textField(self.object_tf,q=True,tx=True)
        attr = mc.textScrollList(self.obj_attr_tcl,q=True,si=True)
        if attr:
            outputList =  acc.findSdkNode_output(con.lAttr(obj,attr[0]))
            if outputList:
                mc.textScrollList(self.animCurveNode_tcl, e=True, a=outputList)
                # mc.textScrollList(self.animCurveNode_tcl, e=True, sii=1)

        return
    
    def btn_animCurveNode_tcl_selectCmd(self):
        tx = mc.textScrollList(self.animCurveNode_tcl,q=True,si=True)
        if tx:
            mc.select(tx[0])
        return
    
    def btn_refresh_connectObj_tcl(self):
    
        mc.textScrollList(self.connectObj_tcl, e=True, ra=True)
    
        obj = mc.textField(self.object_tf, q=True, tx=True)
        attr = mc.textScrollList(self.obj_attr_tcl, q=True, si=True)
        # print attr
        if attr:
            outputList = acc.find_attrToDriven(obj, attr[0])
            # print outputList
            if outputList:
                mc.textScrollList(self.connectObj_tcl, e=True, a=outputList)
                # mc.textScrollList(self.connectObj_tcl, e=True, sii=1)
        return
    def btn_connectObj_tcl_selectCmd(self):
        tx = mc.textScrollList(self.connectObj_tcl,q=True,si=True)
        if tx:
            mc.select(tx[0])
        return
    
    def _get_tag(self):
        
        lf = mc.textField(self.lf_tag_tf,q=True,tx=True)
        rt = mc.textField(self.rt_tag_tf,q=True,tx=True)
        return lf,rt
    
    def btn_mirrorObjSdk(self):
        
        sobj = mc.textField(self.object_tf,q=True,tx=True)
        if not sobj:
            return
        lf, rt = self._get_tag()
        acc.mirror_objSdk_to(sobj,lf,rt)
        
        return
    
    def btn_exportData(self):
        
        
        
        return
if __name__ == '__main__':
    pass